function wp_preloader_idc() {
    if (
        is_admin() ||
        wp_doing_ajax() ||
        (defined('REST_REQUEST') && REST_REQUEST) ||
        $GLOBALS['pagenow'] === 'wp-login.php'
    ) {
        return;
    }

    $delay = 1000; // milliseconds
    $loader = 'https:..............';
    $overlayColor = '#000000';

    add_action('wp_body_open', function() use ($loader, $overlayColor) {
        ?>
        <div id="preloader-idc">
            <img src="<?php echo esc_url($loader); ?>" alt="Loading">
        </div>
        <?php
    });

    add_action('wp_head', function() use ($overlayColor) {
        ?>
        <style>
            #preloader-idc {
                position: fixed;
                inset: 0;
                background-color: <?php echo esc_attr($overlayColor); ?>;
                z-index: 999999;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            #preloader-idc img {
                height: 150px;
            }
            body.preloader-active {
                overflow: hidden;
            }
        </style>
        <?php
    });

    add_action('wp_footer', function() use ($delay) {
        ?>
        <script>
            document.addEventListener("DOMContentLoaded", function () {
                document.body.classList.add("preloader-active");

                setTimeout(function () {
                    const preloader = document.getElementById("preloader-idc");
                    if (preloader) {
                        preloader.remove();
                    }
                    document.body.classList.remove("preloader-active");
                }, <?php echo (int)$delay; ?>);
            });
        </script>
        <?php
    });

}

add_action('template_redirect', 'wp_preloader_idc');